﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SoftVenture.Sharepoint.Permissions.UnitTest
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class FileHelperUnitTest
    {
        private string url = "http://www.rncci.min-saude.pt/News";
        private string docLib = "MapaImagens";
        private string fileName = "mapadistritos.gif";
        string roleName = "Aprovar";
        string roleName2 = "Controlo Total";
        string user = @"LMONTEIRO-DEV\testeuser";

        public FileHelperUnitTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void MyTestInitialize()
        {
            string output = string.Empty;
            FileHelper fh = new FileHelper(url, docLib, fileName);

            fh.ResetRoleInheritance(ref output);

            fh.Dispose();
            System.Diagnostics.Debug.WriteLine(output);
        }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void ListPermission()
        {
            string output = string.Empty;
            fileName = null;

            FileHelper fh = new FileHelper(url, docLib, fileName);

            fh.List(true, ref output);

            System.Diagnostics.Debug.WriteLine(output);

            fh.Dispose();
        }

        #region Single File Tests
        [TestMethod]
        public void AddUserRole()
        {
            string output = string.Empty; 
            
            FileHelper fh = new FileHelper(url, docLib, fileName);

            fh.RoleInheritance = false;
            fh.BreakRoleInheritance(ref output);

            fh.Add(roleName, user, ref output);

            fh.List(false, ref output);
            
            fh.Dispose();
            System.Diagnostics.Debug.WriteLine(output);
            Assert.IsTrue(output.IndexOf( user + Environment.NewLine + "\t\t" + roleName) > 0);
        }
        [TestMethod]
        public void AddUser2Roles()
        {
            string output = string.Empty;

            FileHelper fh = new FileHelper(url, docLib, fileName);

            fh.RoleInheritance = false;
            fh.BreakRoleInheritance(ref output);

            fh.Add(roleName, user, ref output);
            fh.Add(roleName2, user, ref output);

            fh.List(false, ref output);

            fh.Dispose();
            System.Diagnostics.Debug.WriteLine(output);
            Assert.IsTrue(
                output.IndexOf(user + Environment.NewLine + "\t\t" + roleName + Environment.NewLine + "\t\t" + roleName2) > 0 ||
                output.IndexOf(user + Environment.NewLine + "\t\t" + roleName2 + Environment.NewLine + "\t\t" + roleName) > 0);
        }
        [TestMethod]
        public void RemoveSingleRoleUser()
        {

            string output = string.Empty;
            string result = string.Empty;

            FileHelper fh = new FileHelper(url, docLib, fileName);

            fh.RoleInheritance = false;
            fh.BreakRoleInheritance(ref output);

            fh.Add(roleName, user, ref output);

            fh.List(false, ref output);
            fh.List(false, ref result);

            Assert.IsTrue(result.IndexOf(user + Environment.NewLine + "\t\t" + roleName) > 0);

            fh.Remove(roleName, user, ref output);

            fh.List(false, ref output);

            result = string.Empty;
            fh.List(false, ref result);

            fh.Dispose();
            System.Diagnostics.Debug.WriteLine(output);
            Assert.IsTrue(result.IndexOf(user + Environment.NewLine + "\t\t" + roleName) == -1);
           
        }
        [TestMethod]
        public void Remove1o2Roles()
        {

            string output = string.Empty;
            string result = string.Empty;

            FileHelper fh = new FileHelper(url, docLib, fileName);

            fh.RoleInheritance = false;
            fh.BreakRoleInheritance(ref output);

            fh.Add(roleName, user, ref output);
            fh.Add(roleName2, user, ref output);

            fh.List(false, ref output);
            fh.List(false, ref result);

            Assert.IsTrue(
                result.IndexOf(user + Environment.NewLine + "\t\t" + roleName + Environment.NewLine + "\t\t" + roleName2) > 0 ||
                result.IndexOf(user + Environment.NewLine + "\t\t" + roleName2 + Environment.NewLine + "\t\t" + roleName) > 0);
            result = string.Empty;

            fh.Remove(roleName, user, ref output);

            fh.List(false, ref output);
            fh.List(false, ref result);

            fh.Dispose();
            System.Diagnostics.Debug.WriteLine(output);
            Assert.IsTrue(
                result.IndexOf(user + Environment.NewLine + "\t\t" + roleName) == -1 &&
                result.IndexOf(user + Environment.NewLine + "\t\t" + roleName2) > 0 - 1
                );

        }
        [TestMethod]
        public void RemoveAllRoleFromUser()
        {

            string output = string.Empty;
            string result = string.Empty;

            FileHelper fh = new FileHelper(url, docLib, fileName);

            fh.RoleInheritance = false;
            fh.BreakRoleInheritance(ref output);

            fh.Add(roleName, user, ref output);

            fh.List(false, ref output);
            fh.List(false, ref result);

            Assert.IsTrue(result.IndexOf(user + Environment.NewLine + "\t\t" + roleName) > 0);

            result = string.Empty;

            fh.RemoveAll(user, ref output);

            fh.List(false, ref output);
            fh.List(false, ref result);

            fh.Dispose();
            System.Diagnostics.Debug.WriteLine(output);
            Assert.IsTrue(result.IndexOf(user + Environment.NewLine + "\t\t" + roleName) == -1);
        }
        [TestMethod]
        public void Remove2o2RolesFromUser()
        {

            string output = string.Empty;
            string result = string.Empty;

            FileHelper fh = new FileHelper(url, docLib, fileName);

            fh.RoleInheritance = false;
            fh.BreakRoleInheritance(ref output);

            fh.Add(roleName, user, ref output);
            fh.Add(roleName2, user, ref output);

            fh.List(false, ref output);
            fh.List(false, ref result);

            Assert.IsTrue(
                result.IndexOf(user + Environment.NewLine + "\t\t" + roleName + Environment.NewLine + "\t\t" + roleName2) > 0 ||
                result.IndexOf(user + Environment.NewLine + "\t\t" + roleName2 + Environment.NewLine + "\t\t" + roleName) > 0);

            result = string.Empty;

            fh.RemoveAll(user, ref output);

            fh.List(false, ref output);
            fh.List(false, ref result);

            fh.Dispose();
            System.Diagnostics.Debug.WriteLine(output);
            Assert.IsTrue(
                result.IndexOf(user + Environment.NewLine + "\t\t" + roleName + Environment.NewLine + "\t\t" + roleName2)== -1 ||
                result.IndexOf(user + Environment.NewLine + "\t\t" + roleName2 + Environment.NewLine + "\t\t" + roleName) == -1);
        }
        #endregion

        #region Root Folder Tests
        //[TestMethod]
        //public void AddUserRoleToRootFiles()
        //{
        //    string output = string.Empty;
        //    string result = string.Empty;

        //    FileHelper fh = new FileHelper(url, docLib);

        //    fh.Add(roleName, user, ref output);

        //    fh.List(false, ref output);
        //    fh.List(false, ref result);

        //    fh.Dispose();
            
        //    Assert.IsTrue(result.IndexOf(user + Environment.NewLine + "\t\t" + roleName) > 0);
        //}
        #endregion

        #region Full Document Library
        #endregion
    }
}
